<html>
<head><meta charset="utf-8"><title>Why is rust-gpu sound ? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html">Why is rust-gpu sound ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238649324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238649324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238649324">(May 13 2021 at 17:01)</a>:</h4>
<p>I just read on reddit (<a href="https://www.reddit.com/r/rust/comments/mykk7x/gpu_compute_shader_for_sha256_using_rust/">https://www.reddit.com/r/rust/comments/mykk7x/gpu_compute_shader_for_sha256_using_rust/</a>) that rust-gpu allows compute shaders. This is an example: <a href="https://github.com/RustyBamboo/hash-shader/blob/main/vulkan/kernel/src/lib.rs#L322-L342">https://github.com/RustyBamboo/hash-shader/blob/main/vulkan/kernel/src/lib.rs#L322-L342</a></p>
<p>I'm not sure I fully understand how slices work in Rust yet, but I don't really understand why that example is correct.</p>



<a name="238649755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238649755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238649755">(May 13 2021 at 17:04)</a>:</h4>
<p>Doesn't every "thread" in the compute shader get a copy of the <code>hash: &amp;mut [u32]</code> slice ?</p>
<p>I'd have thought that having copies of an exclusive reference on multiple threads was exactly what these were designed to prevent.</p>
<p>Could someone explain how and why this works for someone that's just getting started with Rust?</p>
<p>Is it ok to have multiple mutable references to the same slice in certain situations?</p>



<a name="238649841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238649841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238649841">(May 13 2021 at 17:04)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="119009">@eddyb</span> do you work on this maybe?</p>



<a name="238672213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672213">(May 13 2021 at 19:59)</a>:</h4>
<p><span class="user-mention" data-user-id="409057">@hannahE2</span> the answer is that isn't at the moment, but other features are needed to make it sound aren't available yet, so it's a known issue, and won't look like that forever.</p>



<a name="238672227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672227">(May 13 2021 at 19:59)</a>:</h4>
<p>alright, I see nobody has rep- oh wow that's a race condition and a half</p>



<a name="238672259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672259">(May 13 2021 at 19:59)</a>:</h4>
<p>basically it's WIP, <code>&amp;mut</code> in the interface is almost always going to be wrong</p>



<a name="238672471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672471">(May 13 2021 at 20:01)</a>:</h4>
<p>outputs should be <code>&amp;mut MaybeUninit&lt;T&gt;</code> (but ideally returned, maybe in a <code>struct</code>), not <code>&amp;mut T</code>, and any kind of buffer should be shared references with interior mutability</p>



<a name="238672489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672489">(May 13 2021 at 20:01)</a>:</h4>
<p>think <code>&amp;[AtomicU32]</code> for your example</p>



<a name="238672517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672517">(May 13 2021 at 20:01)</a>:</h4>
<p>(or some kind of wrapper that allows more interesting access patterns)</p>



<a name="238672584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672584">(May 13 2021 at 20:01)</a>:</h4>
<blockquote>
<p>Is it ok to have multiple mutable references to the same slice in certain situations?</p>
</blockquote>
<p>never, as far as I'm concerned</p>



<a name="238672742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238672742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238672742">(May 13 2021 at 20:02)</a>:</h4>
<p><span class="user-mention" data-user-id="409057">@hannahE2</span> btw if you're not already in the Rust-GPU Discord, you might want to join, since that's the main place of discussion - it's not officially supported (yet :P) so mentions of it on Zulip could get lost</p>



<a name="238744957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238744957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238744957">(May 14 2021 at 10:15)</a>:</h4>
<p>discord requires a valid email address, I don't really like sharing mine to third-parties without a good reason</p>



<a name="238744998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238744998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238744998">(May 14 2021 at 10:15)</a>:</h4>
<p>So if the input must be <code>&amp;[AtomicU32]</code>, isn't that like super slow?</p>



<a name="238745083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238745083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238745083">(May 14 2021 at 10:16)</a>:</h4>
<p>i'm just thinking of using atomic loads and stores for all memory accesses that can be written to<br>
IIRC, on GPUs, atomic accesses require device-wide fences that invalidate all caches.</p>



<a name="238745126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238745126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238745126">(May 14 2021 at 10:16)</a>:</h4>
<p>So most people use unordered loads/stores, and then synchronize in some other way (group sync's, etc.)</p>



<a name="238745273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238745273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238745273">(May 14 2021 at 10:18)</a>:</h4>
<p>For someone that wants to learn Rust gpu, is there a good example somewhere of how to implement basic compute kernels correctly and efficiently? I'm think, e.g., a 1D heat conduction kernel with halos and "thread group" local memory, and then also how to implement a prefix-sum so that it matches memcpy in speed.</p>



<a name="238745352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238745352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238745352">(May 14 2021 at 10:19)</a>:</h4>
<p>Ralph had a blogpost about prefix-sum that I really liked showing a decoupled lookback implementation with Vulkan, which is something that can be easily compared with CUDA cub's prefix sum.</p>



<a name="238745984"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238745984" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238745984">(May 14 2021 at 10:26)</a>:</h4>
<p>I'd guess one also has to use atomics for slices in "group private memory" that will be accessed by different threads, right?</p>



<a name="238746067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238746067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238746067">(May 14 2021 at 10:26)</a>:</h4>
<p>Are those atomics "different" than normal <code>core::atomic</code> types ? Using device-wide atomics and fences for group local atomic accesses would be overkill as well.</p>



<a name="238827415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238827415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238827415">(May 14 2021 at 20:47)</a>:</h4>
<p>discord requires <em>a</em> email, it doesn't have to be your real main email.</p>



<a name="238909058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238909058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238909058">(May 15 2021 at 16:26)</a>:</h4>
<p>yeah, doesn't Zulip also require something like that?</p>



<a name="238909191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238909191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238909191">(May 15 2021 at 16:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F/near/238745126">said</a>:</p>
<blockquote>
<p>So most people use unordered loads/stores, and then synchronize in some other way (group sync's, etc.)</p>
</blockquote>
<p>the non-atomic synchronization would require specialized wrapper types that control the kinds of indices you can use to read/write, IIRC. we don't have that yet</p>



<a name="238909333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238909333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238909333">(May 15 2021 at 16:31)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F/near/238745273">said</a>:</p>
<blockquote>
<p>For someone that wants to learn Rust gpu, is there a good example somewhere of how to implement basic compute kernels correctly and efficiently? I'm think, e.g., a 1D heat conduction kernel with halos and "thread group" local memory, and then also how to implement a prefix-sum so that it matches memcpy in speed.</p>
</blockquote>
<p>compute shaders aren't a huge focus right now, so some functionality may be missing, but also the people more knowledgeable than me are on the Rust-GPU Discord</p>



<a name="238909403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238909403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238909403">(May 15 2021 at 16:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409057">hannahE2</span> <a href="#narrow/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F/near/238746067">said</a>:</p>
<blockquote>
<p>Are those atomics "different" than normal <code>core::atomic</code> types ? Using device-wide atomics and fences for group local atomic accesses would be overkill as well.</p>
</blockquote>
<p>you can't use atomics yet even, to be clear (unless we fixed the relevant bugs), I was just describing the "default" case, where you don't have anything more specialized that applies, for the future. right now you have to use <code>&amp;mut [u32]</code> or w/e anyway</p>



<a name="238947057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Why%20is%20rust-gpu%20sound%20%3F/near/238947057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Why.20is.20rust-gpu.20sound.20.3F.html#238947057">(May 16 2021 at 04:00)</a>:</h4>
<p>You can set barriers right now with <code>spirv_std::arch::{memory_barrier, control_barrier}</code>, for atomic ops you can use <code>asm!</code>.</p>
<p>The atomic operations would probably be at least separate functions for gpu atomic ops, but doesn’t have to separate types, as spirv doesn’t have atomic ints (except AtomicCounter, which is its own type) just atomic operations.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>